﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.home.MiniContent
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Vote : IHttpHandler
    {

        private int userID;
        public void ProcessRequest(HttpContext context)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                context.Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            StringBuilder sbHtml = new StringBuilder(context.Cache["user_minicontent"].ToString());//sbContent
            string strUserID = context.Request.QueryString["MiniContentUserID"];
            string strUl = string.Empty;
            if (!string.IsNullOrEmpty(strUserID))
            {
                strUserID = strUserID.Substring(0, strUserID.IndexOf("?"));
                strUl = VoteListByUserID(context, Convert.ToInt32(strUserID));
            }
            else
            {
                strUl = VoteListFriendByUserID(context, userID);
            }
            if (strUl.Trim() == "")
            {
                strUl = "暂无动态!";
            }
            sbHtml = sbHtml.Replace("$ContentMini_ul$",strUl);
            context.Response.ContentType = "text/plain";
            context.Response.Write(sbHtml.ToString());
        }

        private string VoteListFriendByUserID(HttpContext context,int userID)
        {
            int currentpage = 1;//当前页
            int pagesize = 10;//页记录大小
            List<int?> count;
            
            StringBuilder sbList = new StringBuilder();
            string strListli = context.Cache["user_minicontent_ul_vote"].ToString();
            List<Jy123.Model.V_User_VoteList> listVote = new Jy123.BLL.User.Vote().PaginationVoteList_Friend(userID,currentpage, pagesize, "*", "AddTime desc",out count);
            foreach (Jy123.Model.V_User_VoteList vote in listVote)
            {
                string str = strListli;
                str = str.Replace("$VoteID$", vote.ID.ToString());
                str = str.Replace("$UserID$", vote.FUser.ToString());
                str = str.Replace("$UserName$", vote.UserName);

                //判断用户是否有头像
                vote.Portrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(vote.FUser));

                str = str.Replace("$Portrait$", vote.Portrait);
                str = str.Replace("$JoinPersonCount$", vote.JoinCount.ToString());
                str = str.Replace("$VoteTitle$", vote.Title);
                str = str.Replace("$VoteOption$", VoteListOption(context,vote.ID));
                str = str.Replace("$PostTime$",vote.AddTime.ToString());
                sbList.Append(str);
            }

            return sbList.ToString();
        }

        private string VoteListByUserID(HttpContext context, int userID)
        {
            int currentpage = 1;//当前页
            int pagesize = 10;//页记录大小
            List<int?> count;
            StringBuilder sbList = new StringBuilder();
            string strListli = context.Cache["user_minicontent_ul_vote"].ToString();
            List<Jy123.Model.V_User_VoteList> listVote = new Jy123.BLL.User.Vote().PaginationVoteList_MY(userID, currentpage,pagesize, "*", "AddTime desc",out count);
            foreach (Jy123.Model.V_User_VoteList vote in listVote)
            {
                string str = strListli;
                str = str.Replace("$VoteID$", vote.ID.ToString());
                str = str.Replace("$UserID$", vote.FUser.ToString());
                str = str.Replace("$UserName$", vote.UserName);

                //判断用户是否有头像
                vote.Portrait = new Jy123.BLL.User.SPace().DefaultPortrait(Convert.ToInt32(vote.FUser));

                str = str.Replace("$Portrait$", vote.Portrait);
                str = str.Replace("$JoinPersonCount$", vote.JoinCount.ToString());
                str = str.Replace("$VoteTitle$", vote.Title);
                str = str.Replace("$VoteOption$", VoteListOption(context, vote.ID));
                str = str.Replace("$PostTime$", vote.AddTime.ToString());
                sbList.Append(str);
            }
            return sbList.ToString();
        }

        private string VoteListOption(HttpContext context,int VoteID)
        {
            StringBuilder sbList = new StringBuilder();
            string strHtml = context.Cache["user_votelist_option"].ToString();
            DataTable dtVoteOption = new Jy123.BLL.User.Vote().VoteOptionList(VoteID, 1, 3, "*", "").Tables[0];
            //List<Jy123.Model.User_VoteOption> listOption = new Jy123.BLL.User_VoteOption().GetModelList("FVote='"+VoteID+"'");
            for (int i = 0; i < dtVoteOption.Rows.Count; i++)
            {
                string strRow = strHtml;
                strRow = strRow.Replace("$VoteOptionName$", dtVoteOption.Rows[i]["OptionName"].ToString());
                sbList.Append(strRow);
            }
            return sbList.ToString();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
